package com.example.myfinalhomework;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class Main extends AppCompatActivity {
    DBHelper dBhelper;
    TXmanager txManager;
    private static final String TAG = "Main";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toolbar toolbar=  this.findViewById(R.id.toolbar);
        this.setSupportActionBar(toolbar);

        dBhelper = new DBHelper(this);
        txManager = new TXmanager(this);
        SQLiteDatabase db = dBhelper.getReadableDatabase();


        List<userrelated> re = TXmanager.listAll();
        ListView listView=findViewById(R.id.list);
        Log.i(TAG,"1");
        if(re.size()==0) {
            listView.setAdapter(null);
            Log.i(TAG,"2");

        }else{
            Adapter Adapter=new Adapter(Main.this, re);
            listView.setAdapter(Adapter);
            Log.i(TAG,"3");
        }


        FloatingActionButton floatingActionButton=findViewById(R.id.add);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i(TAG,"4");
                Intent intent=new Intent(Main.this, add.class);
                startActivity(intent);
            }
        });



        EditText thename=findViewById(R.id.search);
        thename.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Log.i(TAG, "5");
                String title = thename.getText().toString();
                List<userrelated> tempList = new ArrayList<>();
                if (title.isEmpty()) {
                    tempList = TXmanager.listAll();
                } else {
                    userrelated tempUser = TXmanager.getByName(title);
                    if (tempUser != null) {
                        tempList.add(tempUser);
                    } else {
                        Log.i(TAG, "没有找到匹配的用户：" + title);
                    }
                }
                Adapter adapter = new Adapter(Main.this, tempList);
                listView.setAdapter(adapter);
                Log.i(TAG, "6");
            }
        });
    }
}